package com.search;

import org.apache.lucene.store.Directory;
import org.apache.lucene.store.jdbc.dialect.MySQLDialect;
 
import junit.framework.TestCase;
 
/**
 * The Class LuceneJDBCTest.
 * @author prabhat.jha
 */
public class LuceneJDBCTest extends TestCase {
 
    /** The directory. */
    private Directory   directory   = null;
 
       /** (non-Javadoc)
     * @see junit.framework.TestCase#setUp()
     */
    protected void setUp() throws Exception {
        directory = new MyJDBCDirectory(JDBCDatabaseUtil.getDataSource(), new MySQLDialect(), "LUCENE_INDEX_TABLE");
        super.setUp();
    }
 
    /**
     * Test insert record.
     */
    public void testInsertRecord() {        
        new JDBCBatchInsert().insertRecords();
    }
 
    /**
     * Test build index.
     */
    public void testBuildIndex() {
        new JDBCIndexer(directory).buildIndex();
    }
 
    /**
     * Test search record on name.
     */
    public void testSearchRecordOnName() {
        boolean found = new JDBCSearcher(directory).search("name", "Spring In Action");
        assertEquals(found, true);
    }
 
    /**
     * Test search record fail on name.
     */
    public void testSearchRecordFailOnName() {
        boolean found = new JDBCSearcher(directory).search("name", "No Such BookName");
        assertEquals(found, false);
    }
 
    /**
     * Test search record on author.
     */
    public void testSearchRecordOnAuthor() {
        boolean found = new JDBCSearcher(directory).search("author", "Test Author Hibernate In Action10");
        assertEquals(found, true);
    }
 
    /**
     * Test search record fail on author.
     */
    public void testSearchRecordFailOnAuthor() {
        boolean found = new JDBCSearcher(directory).search("name", "No Such Author");
        assertEquals(found, false);
    }
 
    /**
     * Test search record on publisher.
     */
    public void testSearchRecordOnPublisher() {
        boolean found = new JDBCSearcher(directory).search("publisher", "Test Publisher Spring Bible7");
        assertEquals(found, true);
    }
 
    /**
     * Test search record fail on publisher.
     */
    public void testSearchRecordFailOnPublisher() {
        boolean found = new JDBCSearcher(directory).search("name", "No Such Publisher");
        assertEquals(found, false);
    }
     
    /* (non-Javadoc)
     * @see junit.framework.TestCase#tearDown()
     */
    protected void tearDown() throws Exception {
        if(directory != null) {
            directory.close();
        }
        super.tearDown();
    }
}